package com.wcfsy.wechat.wx.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.wcfsy.wechat.wx.entity.ReplyMessage;
import com.wcfsy.wechat.wx.sys.dao.WechatConfigDao;
import com.wcfsy.wechat.wx.sys.dao.WechatQrCodeDao;
import com.wcfsy.wechat.wx.util.Constant;
import com.wcfsy.wechat.wx.util.DateStyle;
import com.wcfsy.wechat.wx.util.DateUtil;
import com.wcfsy.wechat.wx.util.HttpUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;
import com.wcfsy.wechat.wx.util.WechatUtil;

@Service("weChatQrCodeService")
public class WeChatQrCodeService {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private WechatUtil wechatUtil;
	@Resource
	private WechatQrCodeDao wechatQrCodeDao;
	@Resource
	private WechatConfigDao wechatConfigDao;

	public JsonMessage createQrCode(String channel) {

		if (StringUtils.isNotEmpty(channel)) {
			String access_token = wechatUtil.getToken().getString("access_token");
			if (access_token != null) {
				JSONObject postRequest = new JSONObject();
				JSONObject scene = new JSONObject();
				JSONObject scene_str = new JSONObject();
				scene_str.put("scene_str", channel);
				scene.put("scene", scene_str);
				postRequest.put("action_name", Constant.ACTION_NAME);
				postRequest.put("action_info", scene);
				try {
					logger.info("request param : " + postRequest.toString()
							+ "\n");
					String result = HttpUtil.getInstance().phpPost(
							Constant.QR_CODE + access_token, postRequest);
					logger.info(" result : " + result);
					JSONObject resultObject = (JSONObject) JSONObject
							.parseObject(result);
					String ticket = resultObject.getString("ticket");
					if (StringUtils.isNotEmpty(ticket)) {
						String getQrticketUrl = Constant.GET_TICKET_URL
								+ ticket;
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("qrImage", getQrticketUrl);
						map.put("ticket", ticket);
						map.put("channel", channel);
						map.put("createDate", DateUtil.DateToString(new Date(),
								DateStyle.YYYY_MM_DD_HH_MM_SS));
						wechatQrCodeDao.saveQRImageAndTicket(map);
						return JsonMessage.getSuccessJson(getQrticketUrl);
					} else {
						return JsonMessage.getErrorJson("ticket error");
					}
				} catch (Exception e) {
					logger.error("qr error", e);
					return JsonMessage.getErrorJson("qr error");
				}
			} else {
				logger.error("token is null ");
				return JsonMessage.getErrorJson("token is null");
			}
		} else {
			logger.error("channel is null");
			return JsonMessage.getErrorJson("channel is null");
		}
	}
	

	/** 
	* @Title: saveQrCodeEvent 
	* @Description: 保存回调信息
	* @param @param map
	* @param @return   
	* @return int   
	* @throws 
	*/
	public int saveQrCodeEvent(Map<String,Object> map){
		return wechatQrCodeDao.saveQrCodeEvent(map);
	}
	
	/** 
	* @Title: getWechatUser 
	* @Description: 获取用户渠道
	* @param @return   
	* @return WeChatQrCode   
	* @throws 
	*/
	public List<ReplyMessage> getWechatUser(String openId){
		return wechatQrCodeDao.getWechatUser(openId);
	}
	
	/** 
	* @Title: getWechatUser 
	* @Description: 获取用户渠道
	* @param @return   
	* @return WeChatQrCode   
	* @throws 
	*/
	public ReplyMessage getReplyMessage(String param){
		return wechatQrCodeDao.getReplyMessage(param);
	}

}
